package com.feishi.core.analytic.packing;

import com.feishi.core.analytic.PoType;
import com.feishi.core.analytic.body.AnalyticBody;
import com.feishi.core.analytic.element.*;
import com.feishi.core.analytic.packing.contentpacking.AnalyticContentBodyPacking;
import com.feishi.core.analytic.packing.contentpacking.AnalyticContentMapPacking;
import com.feishi.core.analytic.packing.contentpacking.AnalyticContentValuePacking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by caixq on 5/4 004.
 */
public class AnalyticBuildPacking extends AnalyticPacking {
    private boolean lengthStatic;
    private int staticLength;
    Map<String,AnalyticContentMapElement> contentMap=new HashMap<>();
    Map<String,AnalyticContentBodyElement> contentBodyMap=new HashMap<>();
    Map<String,AnalyticContentValueElement> contentValueMap=new HashMap<>();

    public AnalyticBuildPacking(AnalyticBody analyticBody) {
        super(analyticBody);
        init();
    }



    List<AnalyticContentMapElement> contentList=new ArrayList<>();
    Map<String,Integer> indexMap=new HashMap<>();


    private void init() {
        AnalyticStaticContentLengthDataElement contentLength=null;
        List<AnalyticStaticContentTypeDataElement> contentTypes=new ArrayList<>();
        AnalyticStaticInputDataElement contentEncode=null;
        AnalyticStaticContentNumberDataElement contentNumber=null;
        int i=0;
        this.staticLength=0;
        this.lengthStatic=true;
        for (AnalyticElement element:analyticBody.getElements()) {

            indexMap.put(element.getName(),i++);

            if(element instanceof AnalyticStaticContentLengthDataElement){
                contentLength = (AnalyticStaticContentLengthDataElement) element;
            }
            if(element.getType()==PoType.ELEMENT_CONTENT_TYPE){
                contentTypes.add((AnalyticStaticContentTypeDataElement) element);
            }
            if(element.getType()==PoType.ELEMENT_CONTENT_ENCODE){
                contentEncode= (AnalyticStaticInputDataElement) element;
            }
            if(element instanceof AnalyticStaticContentNumberDataElement){
                contentNumber= (AnalyticStaticContentNumberDataElement) element;
            }



            if(element instanceof AnalyticStaticDataElement ){
                AnalyticStaticDataElement ele= (AnalyticStaticDataElement) element;
                this.staticLength+=ele.getLength();
                if(ele.needForceDecode()){
                    this.lengthStatic=false;
                }
            }else{
                this.lengthStatic=false;
            }


            if(element instanceof AnalyticContentMapElement){
                AnalyticContentMapElement content= (AnalyticContentMapElement) element;
                contentMap.put(contentTypes.get(0).getName(),content);
                content.init(contentTypes,contentLength,contentEncode);
            }else if(element instanceof AnalyticContentBodyElement){
                AnalyticContentBodyElement content= (AnalyticContentBodyElement) element;
                content.init(contentNumber);
                contentBodyMap.put(contentNumber.getName(),content);
            }else if(element instanceof AnalyticContentValueElement){
                AnalyticContentValueElement content= (AnalyticContentValueElement) element;
                content.init(contentNumber,contentLength);
                String name=contentNumber!=null?contentNumber.getName():(contentLength!=null?contentLength.getName():content.getName());
                contentValueMap.put(name,content);
            }
        }
    }
    public List<AnalyticElement> getElements(){
        return analyticBody.getElements();
    }

    public AnalyticElement getAnalyticElement(String name){
        return analyticBody.getElements().get(indexMap.get(name));
    }

    public AnalyticDataPacking analyticDataPacking(){
        return new AnalyticDataPacking(this);
    }

    public List<AnalyticContentMapElement> getContentList() {
        return contentList;
    }

    public void setContentList(List<AnalyticContentMapElement> contentList) {
        this.contentList = contentList;
    }

    public Map<String, Integer> getIndexMap() {
        return indexMap;
    }

    public void setIndexMap(Map<String, Integer> indexMap) {
        this.indexMap = indexMap;
    }
    public AnalyticContentMapPacking getAnalyticContentMapPacking(String name) {

        return new AnalyticContentMapPacking(contentMap.get(name));
    }
    public AnalyticContentValuePacking getAnalyticContentValuePacking(String name) {

        return new AnalyticContentValuePacking(contentValueMap.get(name));
    }
    public AnalyticContentBodyPacking getAnalyticContentBodyPacking(String name) {

        return new AnalyticContentBodyPacking(contentBodyMap.get(name));
    }

    public AnalyticContentMapElement getAnalyticContentElement(String name) {
        return contentMap.get(name);
    }

    public boolean isLengthStatic() {
        return this.lengthStatic;
    }

    public int getStaticLength() {
        return this.staticLength;
    }

    public AnalyticElement getElement(String name) {
        return this.analyticBody.getElements().get(this.indexMap.get(name));
    }

    public boolean hasContentValuePacking(String name) {
       return this.contentValueMap.containsKey(name);
    }
}
